package com.wf.course.service.impl;

import java.io.File;
import java.io.IOException;
import java.util.List;

import com.wf.course.web.alioss.FileUploader;
import it.sauronsoftware.jave.Encoder;
import it.sauronsoftware.jave.MultimediaInfo;
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.google.gson.Gson;
import com.utils.page.PageBean;
import com.wf.course.dao.ICourseChapterMapper;
import com.wf.course.model.course.CourseChapter;
import com.wf.course.service.ICourseChapterService;
import com.wf.course.web.Constants;
import com.wf.course.web.OSSUtil;
import com.wf.course.web.PutObjectProgressListener;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

@Service
@Transactional(rollbackFor=Exception.class)
public class ICourseChapterServiceImpl implements ICourseChapterService{
	
	@Autowired
	private ICourseChapterMapper mapper;

	@Override
	public void indexInfo(PageBean<CourseChapter> pageBean) {
		// TODO Auto-generated method stub
		pageBean.setResults(this.mapper.queryList(pageBean));
		pageBean.setTotalCount(this.mapper.getTotalCount(pageBean));
	}

	@Override
	public void saveOrUpdateCourseChapter(CourseChapter courseChapter, MultipartFile videoFile,MultipartFile imgFile) {
		// TODO Auto-generated method stub
//
//		// 排序
//		if (null != courseChapter.getOrderNum()) {
//			if (null != courseChapter.getId()) {
//				if (!mapper.queryCourseChapterById(courseChapter.getId()).getOrderNum().equals(courseChapter.getOrderNum())) {
//					if (mapper.queryCourseChaptersByCourseIdAndOrderNum(courseChapter).size() > 0) {
//						mapper.updateCourseChapterOrderNum(courseChapter);
//					}
//				}
//			} else {
//				if (mapper.queryCourseChaptersByCourseIdAndOrderNum(courseChapter).size() > 0) {
//					mapper.updateCourseChapterOrderNum(courseChapter);
//				}
//			}
//		}
		//添加
			if(courseChapter.getId()==null){
 
				//广告图片
				if (!imgFile.isEmpty()) {
					String imgPath = "";
					try {
						imgPath = OSSUtil.addFile(imgFile, "wf_course/imgPath", Constants.IGM_BUCKET);
					} catch (IOException e) {
						e.printStackTrace();
					}
					courseChapter.setImgPath(imgPath);
				}
				this.mapper.saveCourseChapter(courseChapter);
			}else{
			 //修改
				
				if (imgFile!=null && imgFile.getSize() != 0) {
					CourseChapter cpt = mapper.queryCourseChapterById(courseChapter.getId());
					try{
						if(StringUtils.isNotBlank(cpt.getImgPath())){
							// 删除原来图片
							OSSUtil.delete(cpt.getImgPath(), Constants.IGM_BUCKET);
						}
					}catch(Exception e){
						System.out.println(e.getMessage());
					}
					// 重新上传
					// 上传
					String imgPath = "";
					try {
						imgPath = OSSUtil.addFile(imgFile, "wf_course/imgPath", Constants.IGM_BUCKET);
					} catch (IOException e) {
						e.printStackTrace();
					}
					courseChapter.setImgPath(imgPath);
				}
				
				
			  this.mapper.updateCourseChapter(courseChapter);
				
			}
	}

	@Override
	public CourseChapter queryCourseChapterById(Integer id) {
		// TODO Auto-generated method stub
		return this.mapper.queryCourseChapterById(id);
	}

	@Override
	public void deleteById(Integer id) {
		// TODO Auto-generated method stub
		this.mapper.deleteById(id);
	}
	
	
	 @Override
	 public List<CourseChapter> queryCourseChapters(){
		 
		 return this.mapper.queryCourseChapters();
		 
	 }
	 @Override
		public void uploadVideo(CourseChapter courseChapter, MultipartFile videoFile){
			if (videoFile.getSize() != 0) {
				CourseChapter cpt = mapper.queryCourseChapterById(courseChapter.getId());
				try{

					CommonsMultipartFile cmf = (CommonsMultipartFile) videoFile;
					DiskFileItem dfi = (DiskFileItem) cmf.getFileItem();
					File f = dfi.getStoreLocation();
					MultimediaInfo m = new Encoder().getInfo(f);

					long time = m.getDuration() / 1000;
					long hours = time / 3600;
					long minutes = (time % 3600) / 60;
					long seconds = (time % 3600) % 60;

					StringBuffer duration = new StringBuffer();
					if (hours < 10) {
						duration.append("0");
					}
					duration.append(hours + ":");
					if (minutes < 10) {
						duration.append("0");
					}
					duration.append(minutes + ":");
					if (seconds < 10) {
						duration.append("0");
					}
					duration.append(seconds);
					courseChapter.setChapterDuration(duration.toString());

					if(StringUtils.isNotBlank(cpt.getVideoPath())){
						// 重新上传
						String videoPath = FileUploader.fileUpload(videoFile);
						courseChapter.setVideoPath(videoPath);
						// 删除原来视频
						OSSUtil.delete(cpt.getVideoPath(), Constants.IGM_BUCKET);
					}else{
						// 重新上传
						String videoPath = FileUploader.fileUpload(videoFile);
						courseChapter.setVideoPath(videoPath);
					}
				}catch(Exception e){
					e.printStackTrace();
				}
				
			}
			  this.mapper.updateCourseChapter(courseChapter);
	 }

	public void updateChapterSaleNum(CourseChapter courseChapter) {

			this.mapper.updateChapterSaleNum(courseChapter);


	}

	@Override
	public String queryCourseChaptersByCourseId(Integer courseId) {
		// TODO Auto-generated method stub
		return new Gson().toJson(mapper.queryCourseChaptersByCourseId(courseId));
	}

	@Override
	public List<CourseChapter> courseChapterByCourseId(Integer courseId) {
		// TODO Auto-generated method stub
		return mapper.queryCourseChaptersByCourseId(courseId);
	}

}
